package utils;

/**
 * @author duncan
 */
public abstract class Order
{
    /**
     * Orders the specified list by name, and returns the modified list.
     *
     * @param list The list to rearrange.
     * @return A new, modified list.
     */
    public static String[] byName(String[] list)
    {
	//If the list contains less than 2 items,
	//then return the list without doing anything
	//since it's alredy in alphabetic order.
	if (list.length < 2) return list;

	//If the method is still running, then go
	//thorugh all items in the list and check
	//if they should be one step afte rthe next.
	for (int i = 1; i < list.length; i++)
	{
	    list[i]	= list[i].trim();
	    list[i - 1] = list[i - 1].trim();
	    list[i]	= removeAllButCharacters(list[i]);
	    list[i - 1]	= removeAllButCharacters(list[i - 1]);
	    if (list[i].compareToIgnoreCase(list[i - 1]) < 0)
	    {
		changePosition(list, i, i - 1);
		return byName(list);
	    }
	    System.out.println(list[i].compareToIgnoreCase(list[i - 1]));
	}

	//Return the modified list.
	return list;
    }

    /**
     * Change the location of an item in a list of Strings. If there is already
     * something there, the items will swap locations with eachother.
     *
     * @param list The original list
     * @param index1 Index of the item to move
     * @param index2 Index of the position to place it.
     * @return A new modified list.
     */
    public static Object[] changePosition(Object[] list, int index1, int index2)
    {
	Object temp = list[index2];
	list[index2] = list[index1];
	list[index1] = temp;
	return list;
    }

    /**
     * Removes everything in the string except A-Z, a-z, 0-9.
     *
     * @param text The text to modify.
     * @return A new, modified text.
     */
    public static String removeAllButCharacters(String text)
    {
	return text.replaceAll("/[^a-zA-Z0-9]+/g", "");
    }
}
